package at.fhooe.stefanirndorfer.timer;

import org.lwjgl.Sys;

public class Timer {

	/** time at last frame */
	long lastFrame;
	
	/** frames per second */
	int fps;
	
	/** last fps time */
	long lastFPS;
	
	String fpsString;
	
	public Timer(){
		fpsString = "";
	}

	/** 
	 * Calculate how many milliseconds have passed 
	 * since last frame.
	 * 
	 * @return milliseconds passed since last frame 
	 */
	public int getDelta() {
	    long time = getTime();
	    int delta = (int) (time - lastFrame);
	    lastFrame = time;
	 
	    return delta;
	}
	
	/**
	 * Get the accurate system time
	 * 
	 * @return The system time in milliseconds
	 */
	public long getTime() {
	    return (Sys.getTime() * 1000) / Sys.getTimerResolution();
	}
	
	/**
	 * Calculate the FPS and set it in the title bar
	 */
	public String updateFPS() {
		
		if (getTime() - lastFPS > 1000) {
			fpsString = "FPS: " + fps;
			fps = 0;
			lastFPS += 1000;
		}
		fps++;
		return fpsString;
	}
}
